<?php
/*
 * scrumbrella - Scrum board.
 *
 * Copyright (C) 2009  Gustavo N. Borgoni <gborgoni@voicemedia.com.ar>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

include("db_schema.php");

/*
 *
 *
 *
 *
 */
function task_list($project)
{
	global $backlog_task;
	global $project_backlog_table;
	global $user_table;
	global $unit_table;

	$SQL = "SELECT a.id_pbacklog, c.priority, a.estimated, b.name, b.lastname, a.summary, a.last_modified, d.name as unit  FROM $backlog_task a
		LEFT JOIN $user_table b on b.id_user = a.id_user 
		LEFT JOIN $project_backlog_table c ON c.id_pbacklog = a.id_pbacklog
		LEFT JOIN $unit_table d ON d.id_unit = a.id_unit
		WHERE
		c.id_project = $project 
		ORDER BY c.priority DESC";
	$res = db_query($SQL);

	if (db_error($res)) {
		logger_fatal("Error while list tasks " . db_error_message($res));
		return -1;
	}

	if (db_numrows($res) > 0) {
		$task_count = 0;
		while ($row = db_fetchrow($res)) {
			foreach ($row as $key => $value) {
				$task[$task_count][$key] = $value;
			}
			$task_count++;
		}
		db_freeresult($res);
		return $task;
	} else {
		return 0;
	}
}


/*
 *
 *
 *
 */
function task_add($pbacklog, $estimated, $unit, $user)
{
	global $backlog_task;
	global $project_backlog_table;

	$SQL = "SELECT * FROM $project_backlog_table WHERE id_pbacklog=$pbacklog";
	$res = db_query($SQL);

	if (db_error($res)) {
		logger_fatal("Error while select project backlog " . db_error_message($res));
		return -1;
	}
	
	if (db_numrows($res) > 0) {
		$row = db_fetchrow($res);
		
		// Insert the new task
		$SQL = "INSERT INTO $backlog_task VALUES($pbacklog,$user,'{$row['summary']}','{$row['description']}','".
			date("y-m-d h:i:s")."', $unit, $estimated)";
		$res = db_query($SQL);

		return 1;
	} else {
		logger_fatal("Error, project backlog not exits");
		return -1;
	}

}
?>
